package com.csit.action;

import java.util.Date;

import javax.annotation.Resource;

import org.apache.struts2.convention.annotation.ParentPackage;

import com.csit.entity.Sale;
import com.csit.entity.User;
import com.csit.service.SaleDetailService;
import com.csit.service.SaleService;
import com.csit.util.DateUtils;
import com.csit.util.ResponseMsgUtil;
import com.opensymphony.xwork2.ModelDriven;

@ParentPackage("basePackage")
public class SaleAction extends BaseAction implements ModelDriven<Sale> {

	private static final long serialVersionUID = -2059876095776359628L;

	@Resource
	private SaleService saleService;
	
	@Resource
	private SaleDetailService saleDetailService;
	
	private Sale sale =  new Sale();
	
	private String productids;
	
	private String qtys;
	
	private String prices;
	
	private String isFrees;
	
	private String notes;
	
	private Date beginDate;
	
	private Date endDate;
	
	public Sale getModel() {
		return sale;
	}

	public void add(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		try {
			sale.setShop(getShop());
			msgUtil = saleService.addSale(sale, productids, qtys, prices, isFrees, notes);
		} catch (Exception e) {
			e.printStackTrace();
			msgUtil.setMsg("添加销售后台出错");
		}
		ajaxJson(msgUtil.toJSON());
	}
	
	public void delete(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(false);
		try {
			msgUtil = saleService.delete(sale.getSaleID());
		} catch (Exception e) {
			e.printStackTrace();
			msgUtil.setMsg("删除销售后台出错");
		}
		ajaxJson(msgUtil.toJSON());
	}
	
	public void select(){
		sale.setShop(getShop());
		String jsonString = saleService.querySale(page, rows, sale, beginDate, endDate);
		ajaxJson(jsonString);
	}
	
	public void get(){
		ResponseMsgUtil msgUtil = new ResponseMsgUtil(true);

		String saleCode = saleService.getCode(getShop());
		msgUtil.setProperty("saleCode", saleCode);
		msgUtil.setProperty("saleDate", DateUtils.getDateString(new Date()));
		msgUtil.setProperty("saleer", getSession(User.LOGINNAME));
		
		ajaxJson(msgUtil.toJSON());
	}
	
	/**
	 * 
	 * @Description: 获取详情
	 * @param
	 * @Create: 2012-12-8 下午03:24:23
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void getDetail(){
		String jsonString = saleService.getDetailList(sale);
		ajaxJson(jsonString);
	}
	
	/**
	 * 
	 * @Description: 汇总查询
	 * @param
	 * @Create: 2012-12-8 下午03:24:15
	 * @author longweier
	 * @update logs
	 * @return
	 * @throws Exception
	 */
	public void query(){
		String jsonString = saleDetailService.querySaleDetail(page, rows, sale,productids,beginDate, endDate);
		ajaxJson(jsonString);
	}
	
	public String getProductids() {
		return productids;
	}

	public void setProductids(String productids) {
		this.productids = productids;
	}

	public String getQtys() {
		return qtys;
	}

	public void setQtys(String qtys) {
		this.qtys = qtys;
	}

	public String getPrices() {
		return prices;
	}

	public void setPrices(String prices) {
		this.prices = prices;
	}

	public String getIsFrees() {
		return isFrees;
	}

	public void setIsFrees(String isFrees) {
		this.isFrees = isFrees;
	}

	public String getNotes() {
		return notes;
	}

	public void setNotes(String notes) {
		this.notes = notes;
	}

	public Date getBeginDate() {
		return beginDate;
	}

	public void setBeginDate(Date beginDate) {
		this.beginDate = beginDate;
	}

	public Date getEndDate() {
		return endDate;
	}

	public void setEndDate(Date endDate) {
		this.endDate = endDate;
	}

}
